# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=xlrd
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.2.0
pkgrel=2
pkgdesc="Library for developers to extract data from Microsoft Excel (tm) spreadsheet files (mingw-w64)"
arch=('any')
license=('custom')
url="https://github.com/python-excel/xlrd"
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/python-excel/xlrd/archive/${pkgver}.tar.gz)
sha256sums=('F3D25B39A9BDB273B4F13B2847F555EEF029983BDCD2B6283C81F4A93F45B827')

build() {
  cd "${srcdir}"
  rm -rf python{2,3}-build
  for builddir in python{2,3}-build; do
    cp -r ${_realname}-${pkgver} ${builddir}
    pushd $builddir
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py build
    popd
  done
}

package_python3-xlrd() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  install -Dm644 docs/licenses.rst "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/licenses.rst"

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for filename in runxlrd; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.py
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.py "${pkgdir}${MINGW_PREFIX}"/bin/${filename}-py3.py
  done
}

package_python2-xlrd() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  install -Dm644 docs/licenses.rst "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/licenses.rst"

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for filename in runxlrd; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.py
  done
}

package_mingw-w64-i686-python2-xlrd() {
  package_python2-xlrd
}

package_mingw-w64-i686-python3-xlrd() {
  package_python3-xlrd
}

package_mingw-w64-x86_64-python2-xlrd() {
  package_python2-xlrd
}

package_mingw-w64-x86_64-python3-xlrd() {
  package_python3-xlrd
}
